/** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
**/


function Search() {
	this.container = get("search_screen");
	this.searchText = get("search_text");
	this.searchBtn = get("search_btn");
	this.searchError = get("search_error");
	this.hideSearch = get("btn_hide_search");
	
	//insert translated text into search container
	get("search_btn_text").text(str_search_btn);
	get("search_text").val(str_search_text);

	this.searchText.focus(function () {
		if (!this.origValueSet) {
			this.origValue = this.value;
			this.value = "";
			this.origValueSet = true;
		}
	});
	
	this.searchText.blur(function () {
		if (!this.value) {
			this.value = this.origValue;
			this.origValueSet = false;
		}
	});
	
	var self = this;
	this.searchText.keydown(function(ev) {
		if(ev.which == 13)
			self.searchBtn.click();
	});
	this.searchBtn.click(function() { self.search(self.searchText.val()); });
	this.hideSearch.click(this.hide);
}

Search.prototype.show = function () {
	this.searchError.html("");
	this.value = this.searchText[0].origValueSet ? this.searchText[0].origValue : "";
	this.container.removeClass("hidden");
}

Search.prototype.hide = function () {
	get("search_text").val('');
	this.origValueSet = false;
	get("search_screen").addClass("hidden");
}

Search.prototype.search = function(search_text){
	if (search_text && search_text != "" && search_text != str_search_text) {
		this.searchLocalData(search_text);
	} else {
		this.searchError.html(str_search_text_required);
	}
}

Search.prototype.searchOnline = function(search_text, max_search_results, onFinish){
	if(!tabsArray || !tabsArray.length) {
		onFinish(null);
		return;
	}
	
	var param = {
		s: search_text
	}
	
	if(max_search_results) {
		param.max = max_search_results;
	}
	
	var self = this;
	$.ajax({
		type: "GET",
		url: SEARCH_URL,
		data: param,
		dataType: "xml",
		beforeSend: function() { showLoader( true ); },
		complete: function() { showLoader( false ); },
		success: function( xml, status ) { 
			RSS.enableMiniView = false;
			var items = null;
			
			try {
				var tabs = RSS.getTabsFromRss(xml);
				if(tabs && tabs.length)  {
					items = tabs[0].items;
				} 
			} 
			catch (e) {
			}
			
			RSS.enableMiniView = true;
			onFinish(items);

		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			onFinish(null);
		}
	});
}

Search.prototype.searchLocalData = function(search_text) {
	var items = RSS.search(tabsArray, search_text, MAX_SEARCH_RESULTS);
	
	var l = items && items.length ? items.length : 0;
	if (l < MAX_SEARCH_RESULTS && SEARCH_URL) {
		var self = this;
		this.searchOnline(search_text, MAX_SEARCH_RESULTS - l, function(onlineItems){
			if (onlineItems != null) {
				items = items ? jQuery.merge(items, onlineItems) : onlineItems;
			}
			self.addSearchTab(search_text, items);
		});
	}
	else {
		this.addSearchTab(search_text, items);
	}
}

Search.prototype.addSearchTab = function(search_text, items){
	if(!items || !items.length) {
		this.searchError.html(str_search_no_results);
		return;
	}
	
	this.tab = {
		title: "<div class='delete_tab'></div>" + str_search_btn + ": &quot;" + search_text + "&quot;",
		order: -10000000,
		items: items,
		search: true
	};
	
	searchShown = true;
	showEpisodes(null, this.tab, true);
		
	this.hide();
}

Search.prototype.getSearchResults = function() {
	return this.tab;
}
